package com.awspaas.user.apps.cpq.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CPQ_installationRefreshPriceWeb {

	public String installationRefreshPrice(UserContext userContext,String type, String rowData) {
		try {			
			// 获取需要更新的原材料编号。
		    Map<String, JSONObject> userMap = new HashMap<String, JSONObject>();
		    String bms = "";
			List<String> bhList = new ArrayList<>();
			if(type.equals("1")) {
				//进入表单中点击提交按钮
			BO tjBo=	SDK.getBOAPI().query("BO_EU_CPQ_INSTALLATION_FEE").bindId(rowData).detail();
			JSONObject json = new JSONObject();
			String bm = tjBo.getString("BM");
			json.put("DJ", tjBo.getString("DJ"));
			json.put("NAME", tjBo.getString("NAME"));
			json.put("DW", tjBo.getString("DW"));
			json.put("BZ", tjBo.getString("BZ"));
			json.put("BM", tjBo.getString("BZ"));
			bhList.add(bm);
			userMap.put(bm, json);
			bms = "'" + bms + "'";
			}else {
				JSONArray dataList = JSONObject.parseArray(rowData);
				for (int i = 0; i < dataList.size(); i++) {
					JSONObject jsonObject = (JSONObject) dataList.get(i);
					String bm = (String) jsonObject.get("BM");
						JSONObject json = new JSONObject();
						json.put("DJ", (String) jsonObject.get("DJ"));
						json.put("NAME", (String) jsonObject.get("NAME"));
						json.put("DW", (String) jsonObject.get("DW"));
						json.put("BZ", (String) jsonObject.get("BZ"));
						json.put("BM", bm);
						bhList.add(bm);
						userMap.put(bm, json);
						if (bms.isEmpty()) {
							bms = "'" + bms + "'";
						} else {
							bms += ",'" + bms + "'";
						}
				}
			}

			/**
			 * 3、获取需要更新的项目报价记录台账中类型为模板的数据
			 * 
			 */	
			  String allPTsql="select   a.APPLYNO,a.BINDID,a.SBAZFCB,a.AZFZJ,a.AZFLRJE,a.AZFSJ,a.AZF,a.AZFLR,a.AZFSL,a.XMCBYS,a.XMZBJ," + 
				 		"         b.ID,b.XTBM,b.BM,b.SL,b.DJ,b.AZXS," + 
				 		"		  b.ZJ " + 
				 		"from  BO_EU_CPQ_PROJECT_MSG a,BO_EU_CPQ_PROJECT_MSG_IF b " + 
				 		"where  a.BINDID=b.BINDID and a.SFWMB='是' ";
				 String allXTsql="select   a.APPLYNO,a.BINDID," + 
				 		"         b.XTMC,b.DJ,b.ID  " + 
				 		"from  BO_EU_CPQ_PROJECT_MSG a,BO_EU_CPQ_SYSTEM_PRICE b " + 
				 		"where  a.BINDID=b.BINDID and a.SFWMB='是' and b.XTMC='安装费' ";
				 String paramSql="select   a.APPLYNO,a.BINDID," + 
				 		"         b.ID,b.PARMENTIER_NAME,b.PARMENTIER_NO,b.PARMENTIER_VALUE " + 
				 		"from  BO_EU_CPQ_PROJECT_MSG a,BO_EU_CPQ_PROJECT_MSG_PARAMETER_GRIDS b " + 
				 		"where  a.BINDID=b.BINDID and PARMENTIER_VALUE!=''";
				 String allAZFsql="select   a.APPLYNO,a.BINDID," + 
					 		"         b.FYBL,b.FY,b.FYMC,b.ID  " + 
					 		"from  BO_EU_CPQ_PROJECT_MSG a,BO_EU_CPQ_PROJECT_MSG_IFP b " + 
					 		"where  a.BINDID=b.BINDID and a.SFWMB='是'  ";
				 List<RowMap> templateMaps=DBSql.getMaps(allPTsql);
				 List<RowMap> systemMaps=DBSql.getMaps(allXTsql);
				 List<RowMap> paramMaps=DBSql.getMaps(paramSql);
				 List<RowMap> installMaps=DBSql.getMaps(allAZFsql);
				 //组合项目模板中安装模块数据
				 Map<String , List<RowMap>> tempMap=new HashMap<>();
				 if(templateMaps.size()>0) {
					 for (RowMap rowMap : templateMaps) {
						 String wfno=rowMap.getString("APPLYNO");
							if(tempMap.containsKey(wfno)) {
							  List<RowMap> lists= tempMap.get(wfno);
							  lists.add(rowMap);
							  tempMap.put(wfno, lists);
							}else {
								List<RowMap> lists=new ArrayList<RowMap>();
								lists.add(rowMap);
								tempMap.put(wfno, lists);
							}
					}				
				 }
				//组合项目模板中系统安装费价格数据
				 Map<String ,List<RowMap>> systemMap=new HashMap<>();
				 if(systemMaps.size()>0) {
					 for (RowMap rowMap : systemMaps) {
						 String wfno=rowMap.getString("APPLYNO");
							if(systemMap.containsKey(wfno)) {
								 List<RowMap>  lists= systemMap.get(wfno);
							  lists.add( rowMap);
							  systemMap.put(wfno, lists);
							}else {
								List<RowMap> lists=new ArrayList<RowMap>();
								lists.add(rowMap);
								systemMap.put(wfno, lists);
							}
					} 
				 }
					//组合项目模板中参数数据
				 Map<String , List<RowMap>> paramMap=new HashMap<>();
				 if(paramMaps.size()>0) {
					 for (RowMap rowMap : paramMaps) {
						 String wfno=rowMap.getString("APPLYNO");
							if(paramMap.containsKey(wfno)) {
							  List<RowMap> lists= paramMap.get(wfno);
							  lists.add(rowMap);
							  paramMap.put(wfno, lists);
							}else {
								List<RowMap> lists=new ArrayList<RowMap>();
								lists.add(rowMap);
								paramMap.put(wfno, lists);
							}
					}
				 }
					//组合项目模板中安装费用明细数据
				 Map<String , List<RowMap>> installMap=new HashMap<>();
				 if(installMaps.size()>0) {
					 for (RowMap rowMap : installMaps) {
						 String wfno=rowMap.getString("APPLYNO");
							if(installMap.containsKey(wfno)) {
							  List<RowMap> lists= installMap.get(wfno);
							  lists.add(rowMap);
							  installMap.put(wfno, lists);
							}else {
								List<RowMap> lists=new ArrayList<RowMap>();
								lists.add(rowMap);
								installMap.put(wfno, lists);
							}
					}
				 }
				 String  modulSql="update BO_EU_CPQ_PROJECT_MSG_IF set DJ=?,ZJ=?,NAME=?,DW=?,BZ=? "
					 		+ " where BM=? and  ID=?";
				 String  modultrueSql="update BO_EU_CPQ_PROJECT_MSG_IFP set FY=?"
					 		+ " where   ID=?";
				 String tempSql="update BO_EU_CPQ_PROJECT_MSG set SBAZFCB=?,AZFZJ=?,AZFLRJE=?,AZFSJ=?, AZF=?,XMZBJ=?,AZFJZJ=? "
					 		+ " where BINDID=? ";
				 String azfSql="update BO_EU_CPQ_PROJECT_MSG set SBAZFCB=? "
					 		+ " where BINDID=? ";
				 String systemSql="update BO_EU_CPQ_SYSTEM_PRICE set DJ=? "
					 		+ " where ID=?";
				 //遍历项目模板数据，将有关联的数据进行更新
				 for (String  key : tempMap.keySet()) {
					 List<RowMap> lists=tempMap.get(key);
					 RowMap data=lists.get(0);
					 String BH=data.getString("APPLYNO");//项目表单编号	
					 String bindId=data.getString("BINDID");//项目表单编号	
					 String sbazf=data.getString("SBAZFCB");//项目设备安装费成本
					 BigDecimal sbazfnum=new BigDecimal(sbazf);				 	

					 String isTrue="0"; 
					 for (RowMap rowMap : lists) {						 
						//获取安装费模块子表配置明细
							String BM=rowMap.getString("BM");//模块编码	
							String ID=rowMap.getString("ID");//模块信息子表数据ID	
							String xtbm=rowMap.getString("XTMH");//系统编码	
							String SL=rowMap.getString("SL").isEmpty()?"0":rowMap.getString("SL");//数量	
							BigDecimal sl=new BigDecimal(SL);
							//获取该条模块该该项目的原始数据，总价、总材料费、总人工费、总外协、总采购
							String ZJ=rowMap.getString("ZJ");//模块总价
							String AZXS=rowMap.getString("AZXS").isEmpty()?"0":rowMap.getString("AZXS");//安装系数
							String DJ=rowMap.getString("DJ");//模块单价
							BigDecimal zjold=new BigDecimal(ZJ);
							BigDecimal djold=new BigDecimal(DJ);
							BigDecimal azxs=new BigDecimal(AZXS);
							//获取对应更新的安装费模块信息
							if(userMap.containsKey(BM)) {
								JSONObject  json=userMap.get(BM);
								String MKDJ=json.getString("DJ");//安装模块单价
								BigDecimal mdj=new BigDecimal(MKDJ);	
								BigDecimal mndj=mdj.multiply(sl).multiply(azxs);//更新后的总价；
								 //更新当前模块数据
								 DBSql.update(modulSql,new Object[] {mdj,mndj,json.getString("NAME"),json.getString("DW"),json.getString("BZ"),BM,ID});
								 //安装费成本价格计算
								 sbazfnum=sbazfnum.subtract(zjold).add(mndj);
								 isTrue="1";
							}
					 }
					 String AZFS=data.getString("AZFS");//项目安装方式	
					 if(AZFS.equals("指导安装")) {
						 DBSql.update(azfSql,new Object[] {sbazfnum,data.getString("BINDID")}); 
					 }else {
							//更新项目模板费用数据。
						 if(isTrue.equals("1")) {
							 //更新安装费用明细数据
							 BigDecimal zazf=sbazfnum;//总安装费
							 List<RowMap> installs= installMap.get(BH);
							 for (RowMap rowMap : installs) {
								String name= rowMap.getString("FYMC");//费用名称
								String ID=rowMap.getString("ID");
								String FYBL=rowMap.getString("FYBL");//费用比例
								BigDecimal bl=new BigDecimal(FYBL);//费用比例
								BigDecimal num=new BigDecimal("0");
								
								if(name.equals("工程安装费")) {
									 DBSql.update(modultrueSql,new Object[] {sbazfnum,ID});	
								}else {
									if(!FYBL.isEmpty()&&bl.compareTo(num)>0) {
										BigDecimal fy=sbazfnum.multiply(bl);
										 DBSql.update(modultrueSql,new Object[] {fy,ID});
										 zazf=zazf.add(fy);
									}else {
										String FY=rowMap.getString("FY");//费用
										BigDecimal fy=new BigDecimal(FY);//费用比例
										zazf=zazf.add(fy);
									}							
								}
							}
							//更新单栋设备汇总中的安装费费用
							 List<RowMap> systems= systemMap.get(BH);
							 if(systems.size()>0) {
								 for (RowMap rowMap : systems) {
									 
									 DBSql.update(systemSql,new Object[] {zazf,rowMap.getString("ID")});	
									} 
							 }
							//更新项目报价申请单的，设备安装费、总安装费、安装费利润、安装费税费、基准价
							 String AZFLRJE=data.getString("AZFLRJE");//项目安装费利润
							 String AZFLR=data.getString("AZFLR");//项目安装费利润率
							 String AZFSJ=data.getString("AZFSJ");//项目安装费税金
							 String AZFSL=data.getString("AZFSL");//项目安装费税率
							 String XMZBJ=data.getString("XMZBJ");//项目基准价
							 BigDecimal azflr=new BigDecimal(AZFLRJE);	
							 BigDecimal lrl=new BigDecimal(AZFLR);	
							 BigDecimal sj=new BigDecimal(AZFSJ);	
							 BigDecimal azfsl=new BigDecimal(AZFSL);	
							 BigDecimal jzj=new BigDecimal(XMZBJ);	
							 BigDecimal num=new BigDecimal("1");
							 BigDecimal num2=new BigDecimal("100");
							 BigDecimal azfOld=new BigDecimal(sbazf);		
		                     //计算新的安装费利润 成本除以（1-利润率）x利润率
							 BigDecimal azflrNew= zazf.divide(num.subtract(lrl.divide(num2)),4 ).multiply(lrl.divide(num2));						 
							 //计算新的安装费税金
							 BigDecimal sjnew = zazf.add(azflrNew).multiply(azfsl.divide(num2));
							 //新的安装费基准价
							 BigDecimal azjzj = zazf.add(azflrNew).add(sjnew);
							 //计算新的基准价
							 jzj=jzj.subtract(azflr).subtract(sj).subtract(azfOld).add(azflrNew).add(sjnew).add(zazf);
							 /*
							  * 			 String tempSql="update BO_EU_CPQ_PROJECT_MSG set SBAZFCB=?,AZFZJ=?,AZFLRJE=?,AZFSJ=?, AZF=?,XMZBJ=? "
						 		+ " where BINDID=? ";
							  */
							 DBSql.update(tempSql,new Object[] {sbazfnum,zazf,azflrNew,sjnew,zazf,jzj,azjzj,data.getString("BINDID")});	
						 }
						 
					 }						 
				 }
				 ResponseObject ro = ResponseObject.newOkResponse();
					return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
